import pygame
import random

class MapScene:
    def __init__(self, screen_width, screen_height):
        self.screen_width = screen_width
        self.screen_height = screen_height
        
        # 玩家设置
        self.player_size = 40
        self.player_pos = [100, 100]
        self.player_speed = 4
        
        # 加载玩家图片
        self.player_image = pygame.image.load("f:/game/pvp/images/player.jpg")
        self.player_image = pygame.transform.scale(self.player_image, (self.player_size, self.player_size))
        
        # 将战斗按钮移到更上方
        # self.battle_button = pygame.Rect(320, 5, 120, 40)
        self.font = pygame.font.Font(None, 36)
        
        # 地图设置
        self.wall_color = (100, 100, 100)
        self.create_maze()
        
        # 添加背包按钮
        self.inventory_button = pygame.Rect(20, 5, 120, 40)
        self.back_button = pygame.Rect(20, 20, 100, 40)
        
        # 添加野生精灵列表
        self.wild_sprites = []
        self.generate_wild_sprites(3)  # 生成3个野生精灵
        
        # 添加仓库按钮
        self.storage_button = pygame.Rect(160, 5, 120, 40)
        
    def generate_wild_sprites(self, count):
        for _ in range(count):
            # 随机生成位置，确保不在墙内
            while True:
                x = random.randint(100, 650)
                y = random.randint(100, 450)
                if not self.check_collision([x, y]):
                    self.wild_sprites.append({
                        'pos': [x, y],
                        'size': 40,
                        'image': pygame.transform.scale(
                            pygame.image.load("f:/game/pvp/images/enemy.jpg"),
                            (40, 40)
                        )
                    })
                    break

    def draw(self, screen):
        # 绘制背景
        screen.fill((200, 200, 200))
        
        # 绘制所有墙壁
        for wall in self.walls:
            pygame.draw.rect(screen, self.wall_color, wall)
        
        # 绘制野生精灵
        for sprite in self.wild_sprites:
            screen.blit(sprite['image'], sprite['pos'])
        
        # 绘制玩家图片
        screen.blit(self.player_image, self.player_pos)
        
        # 绘制背包按钮
        pygame.draw.rect(screen, (150, 150, 150), self.inventory_button)
        text = self.font.render("invent", True, (0, 0, 0))
        screen.blit(text, (self.inventory_button.x + 35, self.inventory_button.y + 10))
        
        # 绘制仓库按钮
        pygame.draw.rect(screen, (150, 150, 150), self.storage_button)
        text = self.font.render("storage", True, (0, 0, 0))
        screen.blit(text, (self.storage_button.x + 25, self.storage_button.y + 10))
    
    def handle_storage_click(self, pos):
        return self.storage_button.collidepoint(pos)
        
    def check_battle_collision(self):
        player_rect = pygame.Rect(self.player_pos[0], self.player_pos[1], 
                                self.player_size, self.player_size)
        
        for i, sprite in enumerate(self.wild_sprites):
            sprite_rect = pygame.Rect(sprite['pos'][0], sprite['pos'][1], 
                                    sprite['size'], sprite['size'])
            if player_rect.colliderect(sprite_rect):
                # 返回碰撞的精灵索引
                return i
        return None

    def remove_sprite(self, index):
        if 0 <= index < len(self.wild_sprites):
            self.wild_sprites.pop(index)
        
    def draw_back_button(self, screen):
        pygame.draw.rect(screen, (150, 150, 150), self.back_button)
        text = self.font.render("back", True, (0, 0, 0))
        screen.blit(text, (self.back_button.x + 25, self.back_button.y + 10))
        
    def handle_inventory_click(self, pos):
        return self.inventory_button.collidepoint(pos)
        
    def handle_back_click(self, pos):
        return self.back_button.collidepoint(pos)
        
    def handle_click(self, pos):
        # 由于我们移除了战斗按钮，这个方法现在只需要返回 False
        return False

    def create_maze(self):
        # 创建迷宫墙壁
        self.walls = [
            # 外墙
            pygame.Rect(50, 50, 700, 20),    # 上墙
            pygame.Rect(50, 50, 20, 500),    # 左墙
            pygame.Rect(730, 50, 20, 500),   # 右墙
            pygame.Rect(50, 530, 700, 20),   # 下墙
            
            # 内部墙壁
            pygame.Rect(150, 150, 500, 20),  # 横墙1
            pygame.Rect(150, 150, 20, 200),  # 竖墙1
            pygame.Rect(250, 250, 400, 20),  # 横墙2
            pygame.Rect(250, 250, 20, 200),  # 竖墙2
            pygame.Rect(350, 350, 300, 20),  # 横墙3
            pygame.Rect(350, 350, 20, 100),  # 竖墙3
            pygame.Rect(450, 150, 20, 300),  # 竖墙4
        ]

    def check_collision(self, new_pos):
        player_rect = pygame.Rect(new_pos[0], new_pos[1], self.player_size, self.player_size)
        for wall in self.walls:
            if player_rect.colliderect(wall):
                return True
        return False

    def handle_input(self):
        keys = pygame.key.get_pressed()
        new_pos = self.player_pos.copy()
        
        if keys[pygame.K_LEFT]:
            new_pos[0] -= self.player_speed
        if keys[pygame.K_RIGHT]:
            new_pos[0] += self.player_speed
        if keys[pygame.K_UP]:
            new_pos[1] -= self.player_speed
        if keys[pygame.K_DOWN]:
            new_pos[1] += self.player_speed
            
        # 只有在不碰撞的情况下才更新位置
        if not self.check_collision(new_pos):
            self.player_pos = new_pos